﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AddressBook.DAO;
using AddressBook.Entity;
using System.Text;

public partial class SearchContact : System.Web.UI.Page
{
    static Base_EmployeeDAO dao = new Base_EmployeeDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        ResultEntity result = new ResultEntity();
        result.Name = "Contacts";

        if (!string.IsNullOrEmpty(Request.QueryString["Phone"]))
        {
            string where = string.Empty;
            string[] whereArr = new string[5];
            int i = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["FstName"]))
            {
                //where += " FstName like '%" + Request.QueryString["FstName"] + "%'";
                whereArr[i++] = " FstName like '%" + Request.QueryString["FstName"] + "%'";
                whereArr[i++] = " SecName like '%" + Request.QueryString["FstName"] + "%'";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["srcPhone"]))
            {
                //where += " and Phone like '%" + Request.QueryString["Phone"] + "%'";
                whereArr[i++] = " Phone like '%" + Request.QueryString["srcPhone"] + "%'";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["Fax"]))
            {
                //where += " and Fox like '%" + Request.QueryString["Fox"] + "%'";
                whereArr[i++] = " Fax like '%" + Request.QueryString["Fax"] + "%'";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["Email"]))
            {
                // where += " and Email like '%" + Request.QueryString["Email"] + "%'";
                whereArr[i++] = " Email like '%" + Request.QueryString["Email"] + "%'";
            }

            //where = string.Join(" and ", string.Join(";", whereArr).Split(";".ToArray(), StringSplitOptions.RemoveEmptyEntries));
            where = string.Join(" or ", string.Join(";", whereArr).Split(";".ToArray(), StringSplitOptions.RemoveEmptyEntries));

            if (!string.IsNullOrEmpty(where))
            {
                StringBuilder sb = new StringBuilder();

                List<Base_Employee> contacts = dao.GetInfos(" * ", " where isDelete = 0 and " + where + " and left(DeptID,4)=(select top 1 left(DeptID,4) from Base_Employee with(nolock) where phone ='" + Request.QueryString["Phone"] + "')", string.Empty, string.Empty);
                foreach (Base_Employee employee in contacts)
                {
                    sb.Append("{");
                    sb.AppendFormat("\"EmployID\":\"{0}\",", employee.EmployID);
                    sb.AppendFormat("\"DeptID\":\"{0}\",", employee.DeptID);
                    sb.AppendFormat("\"FstName\":\"{0}\",", employee.FstName);
                    sb.AppendFormat("\"SecName\":\"{0}\",", employee.SecName);
                    sb.AppendFormat("\"Phone\":\"{0}\"", employee.Phone);
                    sb.Append("},");
                }
                if (sb.Length > 0)
                {
                    sb.Remove(sb.Length - 1, 1);
                }

                result.ResultCode = "0";
                result.Contents = sb.ToString();
            }
            else
            {
                result.Desc = "没有设置查询条件！";
                result.ResultCode = "-2";
            }
        }
        else
        {
            result.ResultCode = "-2";
            result.Desc = "参数不正确！";
        }

        Response.Write(Common.ReturnResult(result));
    }
}